package com.lantian.uavmaterial.service.impl;

import com.lantian.common.config.LantianConfig;
import com.lantian.common.exception.file.InvalidExtensionException;
import com.lantian.common.utils.StringUtils;
import com.lantian.common.utils.file.FileUploadUtils;
import com.lantian.common.utils.file.FileUtils;
import com.lantian.common.utils.file.MimeTypeUtils;
import com.lantian.common.utils.uuid.UUID;
import com.lantian.uavmaterial.domain.*;
import com.lantian.uavmaterial.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 无人机整机参数信息Service业务层处理
 * 
 * @author lantian
 * @date 2025-11-06
 */
@Service
public class UavEntityServiceImpl implements IUavEntityService
{

    @Autowired
    private IUavTechnicalSpecificationsService uavTechnicalSpecificationsService;
    @Autowired
    private IUavPropulsionSystemService uavPropulsionSystemService;
    @Autowired
    private IUavAirframeConstructionService uavAirframeConstructionService;
    @Autowired
    private IUavBatterySystemService uavBatterySystemService;
    @Autowired
    private IUavCommunicationDatalinkService uavCommunicationDatalinkService;
    @Autowired
    private IUavFlightcontrolSystemService uavFlightcontrolSystemService;
    @Autowired
    private IUavPayloadCameraService uavPayloadCameraService;
    @Autowired
    private IUavTechnicalSpecificationsService iUavTechnicalSpecificationsService;

    @Override
    public int insertUavEntity(UavEntity uavEntity) {
        UavTechnicalSpecifications uavTechnicalSpecifications = uavEntity.getUavTechnicalSpecifications();
        UavBatterySystem uavBatterySystem = uavEntity.getUavBatterySystem();
        UavPropulsionSystem uavPropulsionSystem = uavEntity.getUavPropulsionSystem();
        UavAirframeConstruction uavAirframeConstruction = uavEntity.getUavAirframeConstruction();
        UavFlightcontrolSystem uavFlightcontrolSystem = uavEntity.getUavFlightcontrolSystem();
        UavCommunicationDatalink uavCommunicationDatalink = uavEntity.getUavCommunicationDatalink();
        UavPayloadCamera uavPayloadCamera = uavEntity.getUavPayloadCamera();
        String uavId = UUID.randomUUID().toString();
        String uavBatteryId = null;
        String flightPowerSystemId = null;
        String airframeStructureId = null;
        String flightControlId = null;
        String uavCommunicationId = null;
        String payloadCameraId = null;

        /*保存草稿*/
        String saveDraft = uavTechnicalSpecifications.getSaveDraft();

        if (Objects.nonNull(uavBatterySystem)){
            uavBatteryId = UUID.randomUUID().toString();
            uavBatterySystem.setUavId(uavId);
            uavBatterySystem.setUavBatteryId(uavBatteryId);
            uavBatterySystem.setSaveDraft(saveDraft);
            int res = uavBatterySystemService.insertUavBatterySystem(uavBatterySystem);
        }
        if (Objects.nonNull(uavPropulsionSystem)){
            flightPowerSystemId = UUID.randomUUID().toString();
            uavPropulsionSystem.setUavId(uavId);
            uavPropulsionSystem.setFlightPowerSystemId(flightPowerSystemId);
            uavPropulsionSystem.setSaveDraft(saveDraft);
            int res = uavPropulsionSystemService.insertUavPropulsionSystem(uavPropulsionSystem);
        }
        if (Objects.nonNull(uavAirframeConstruction)){
            airframeStructureId = UUID.randomUUID().toString();
            uavAirframeConstruction.setUavId(uavId);
            uavAirframeConstruction.setAirframeStructureId(airframeStructureId);
            uavAirframeConstruction.setSaveDraft(saveDraft);
            int res = uavAirframeConstructionService.insertUavAirframeConstruction(uavAirframeConstruction);
        }
        if (Objects.nonNull(uavFlightcontrolSystem)){
            flightControlId = UUID.randomUUID().toString();
            uavFlightcontrolSystem.setUavId(uavId);
            uavFlightcontrolSystem.setFlightControlId(flightControlId);
            uavFlightcontrolSystem.setSaveDraft(saveDraft);
            int res = uavFlightcontrolSystemService.insertUavFlightcontrolSystem(uavFlightcontrolSystem);
        }
        if (Objects.nonNull(uavCommunicationDatalink)){
            uavCommunicationId = UUID.randomUUID().toString();
            uavCommunicationDatalink.setUavId(uavId);
            uavCommunicationDatalink.setUavCommunicationId(uavCommunicationId);
            uavCommunicationDatalink.setSaveDraft(saveDraft);
            int res = uavCommunicationDatalinkService.insertUavCommunicationDatalink(uavCommunicationDatalink);
        }
        if (Objects.nonNull(uavPayloadCamera)){
            payloadCameraId = UUID.randomUUID().toString();
            uavPayloadCamera.setUavId(uavId);
            uavPayloadCamera.setPayloadCameraId(payloadCameraId);
            uavPayloadCamera.setSaveDraft(saveDraft);
            int res = uavPayloadCameraService.insertUavPayloadCamera(uavPayloadCamera);
        }
        if (Objects.nonNull(uavTechnicalSpecifications)){
            uavTechnicalSpecifications.setUavId(uavId);
            uavTechnicalSpecifications.setUavBatteryId(uavBatteryId);
            uavTechnicalSpecifications.setFlightPowerSystemId(flightPowerSystemId);
            uavTechnicalSpecifications.setAirframeStructureId(airframeStructureId);
            uavTechnicalSpecifications.setFlightControlId(flightControlId);
            uavTechnicalSpecifications.setUavCommunicationAndDataTransmissionId(uavCommunicationId);
            uavTechnicalSpecifications.setPayloadCameraId(payloadCameraId);
            uavTechnicalSpecifications.setSaveDraft(saveDraft);
            int res = uavTechnicalSpecificationsService.insertUavTechnicalSpecifications(uavTechnicalSpecifications);
        }


        return 1;
    }

    @Override
    public UavEntity insertUavEntity(UavEntity uavEntity, MultipartFile file) {
        UavTechnicalSpecifications uavTechnicalSpecifications = uavEntity.getUavTechnicalSpecifications();
        UavBatterySystem uavBatterySystem = uavEntity.getUavBatterySystem();
        UavPropulsionSystem uavPropulsionSystem = uavEntity.getUavPropulsionSystem();
        UavAirframeConstruction uavAirframeConstruction = uavEntity.getUavAirframeConstruction();
        UavFlightcontrolSystem uavFlightcontrolSystem = uavEntity.getUavFlightcontrolSystem();
        UavCommunicationDatalink uavCommunicationDatalink = uavEntity.getUavCommunicationDatalink();
        UavPayloadCamera uavPayloadCamera = uavEntity.getUavPayloadCamera();
        String uavId = UUID.randomUUID().toString();
        String uavBatteryId = null;
        String flightPowerSystemId = null;
        String airframeStructureId = null;
        String flightControlId = null;
        String uavCommunicationId = null;
        String payloadCameraId = null;

        /*保存草稿*/
        String saveDraft = uavTechnicalSpecifications.getSaveDraft();

        String uavImagePath = null;
        /*图片附件上传*/
        try {
            uavImagePath = FileUploadUtils.upload(LantianConfig.getUavTechnicalSpecificationsPath(), file, MimeTypeUtils.IMAGE_EXTENSION, true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InvalidExtensionException e) {
            throw new RuntimeException(e);
        }

        if (Objects.nonNull(uavBatterySystem)){
            uavBatteryId = UUID.randomUUID().toString();
            uavBatterySystem.setUavId(uavId);
            uavBatterySystem.setUavBatteryId(uavBatteryId);
            uavBatterySystem.setSaveDraft(saveDraft);
            int res = uavBatterySystemService.insertUavBatterySystem(uavBatterySystem);
        }
        if (Objects.nonNull(uavPropulsionSystem)){
            flightPowerSystemId = UUID.randomUUID().toString();
            uavPropulsionSystem.setUavId(uavId);
            uavPropulsionSystem.setFlightPowerSystemId(flightPowerSystemId);
            uavPropulsionSystem.setSaveDraft(saveDraft);
            int res = uavPropulsionSystemService.insertUavPropulsionSystem(uavPropulsionSystem);
        }
        if (Objects.nonNull(uavAirframeConstruction)){
            airframeStructureId = UUID.randomUUID().toString();
            uavAirframeConstruction.setUavId(uavId);
            uavAirframeConstruction.setAirframeStructureId(airframeStructureId);
            uavAirframeConstruction.setSaveDraft(saveDraft);
            int res = uavAirframeConstructionService.insertUavAirframeConstruction(uavAirframeConstruction);
        }
        if (Objects.nonNull(uavFlightcontrolSystem)){
            flightControlId = UUID.randomUUID().toString();
            uavFlightcontrolSystem.setUavId(uavId);
            uavFlightcontrolSystem.setFlightControlId(flightControlId);
            uavFlightcontrolSystem.setSaveDraft(saveDraft);
            int res = uavFlightcontrolSystemService.insertUavFlightcontrolSystem(uavFlightcontrolSystem);
        }
        if (Objects.nonNull(uavCommunicationDatalink)){
            uavCommunicationId = UUID.randomUUID().toString();
            uavCommunicationDatalink.setUavId(uavId);
            uavCommunicationDatalink.setUavCommunicationId(uavCommunicationId);
            uavCommunicationDatalink.setSaveDraft(saveDraft);
            int res = uavCommunicationDatalinkService.insertUavCommunicationDatalink(uavCommunicationDatalink);
        }
        if (Objects.nonNull(uavPayloadCamera)){
            payloadCameraId = UUID.randomUUID().toString();
            uavPayloadCamera.setUavId(uavId);
            uavPayloadCamera.setPayloadCameraId(payloadCameraId);
            uavPayloadCamera.setSaveDraft(saveDraft);
            int res = uavPayloadCameraService.insertUavPayloadCamera(uavPayloadCamera);
        }
        if (Objects.nonNull(uavTechnicalSpecifications)){
            uavTechnicalSpecifications.setUavId(uavId);
            uavTechnicalSpecifications.setUavBatteryId(uavBatteryId);
            uavTechnicalSpecifications.setFlightPowerSystemId(flightPowerSystemId);
            uavTechnicalSpecifications.setAirframeStructureId(airframeStructureId);
            uavTechnicalSpecifications.setFlightControlId(flightControlId);
            uavTechnicalSpecifications.setUavCommunicationAndDataTransmissionId(uavCommunicationId);
            uavTechnicalSpecifications.setPayloadCameraId(payloadCameraId);
            uavTechnicalSpecifications.setSaveDraft(saveDraft);
            uavTechnicalSpecifications.setUavImagePath(uavImagePath);
            int res = uavTechnicalSpecificationsService.insertUavTechnicalSpecifications(uavTechnicalSpecifications);
        }
        uavEntity.setUavTechnicalSpecifications(uavTechnicalSpecifications);
        uavEntity.setUavPayloadCamera(uavPayloadCamera);
        uavEntity.setUavBatterySystem(uavBatterySystem);
        uavEntity.setUavAirframeConstruction(uavAirframeConstruction);
        uavEntity.setUavFlightcontrolSystem(uavFlightcontrolSystem);
        uavEntity.setUavCommunicationDatalink(uavCommunicationDatalink);
        uavEntity.setUavPropulsionSystem(uavPropulsionSystem);
        return uavEntity;
    }

    @Override
    public int deleteUavEntityByUavId(String[] uavIds) {
        uavBatterySystemService.deleteUavBatterySystemByUavBatteryIds(uavIds);
        uavPropulsionSystemService.deleteUavPropulsionSystemByFlightPowerSystemIds(uavIds);
        uavAirframeConstructionService.deleteUavAirframeConstructionByAirframeStructureIds(uavIds);
        uavFlightcontrolSystemService.deleteUavFlightcontrolSystemByFlightControlIds(uavIds);
        uavCommunicationDatalinkService.deleteUavCommunicationDatalinkByUavCommunicationIds(uavIds);
        uavPayloadCameraService.deleteUavPayloadCameraByPayloadCameraIds(uavIds);
        int result = uavTechnicalSpecificationsService.deleteUavTechnicalSpecificationsByUavIds(uavIds);
        return result;
    }

    @Override
    public int deleteUavEntityByUavId(String uavId) {
        /*删除无人机整机信息表对应附件*/
        UavTechnicalSpecifications uavTechnicalSpecifications = uavTechnicalSpecificationsService.selectUavTechnicalSpecificationsByUavId(uavId);
        if (Objects.nonNull(uavTechnicalSpecifications)){
            String uavImagePath = uavTechnicalSpecifications.getUavImagePath();
            if (StringUtils.isNotEmpty(uavImagePath))
            {
                FileUtils.deleteFile(LantianConfig.getProfile() + FileUtils.stripPrefix(uavImagePath));
            }
        }
        uavBatterySystemService.deleteUavBatterySystemByUavBatteryId(uavId);
        uavPropulsionSystemService.deleteUavPropulsionSystemByFlightPowerSystemId(uavId);
        uavAirframeConstructionService.deleteUavAirframeConstructionByAirframeStructureId(uavId);
        uavFlightcontrolSystemService.deleteUavFlightcontrolSystemByFlightControlId(uavId);
        uavCommunicationDatalinkService.deleteUavCommunicationDatalinkByUavCommunicationId(uavId);
        uavPayloadCameraService.deleteUavPayloadCameraByPayloadCameraId(uavId);
        int result = uavTechnicalSpecificationsService.deleteUavTechnicalSpecificationsByUavId(uavId);
        return result;
    }

    @Override
    public int updateUavEntity(UavEntity uavEntity) {
        UavTechnicalSpecifications uavTechnicalSpecifications = uavEntity.getUavTechnicalSpecifications();
        String uavId = uavTechnicalSpecifications.getUavId();
        String saveDraft = uavTechnicalSpecifications.getSaveDraft();
        /*无人机电池*/
        UavBatterySystem uavBatterySystem = uavBatterySystemService.selectUavBatterySystemByUavId(uavId);
        UavBatterySystem uavBatterySystemUp = uavEntity.getUavBatterySystem();
        uavBatterySystemUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavBatterySystem)) {
            uavBatterySystemService.updateUavBatterySystem(uavBatterySystemUp);
        }else {
            uavBatterySystemService.insertUavBatterySystem(uavBatterySystemUp);
        }
        /*无人机动力系统*/
        UavPropulsionSystem uavPropulsionSystem = uavPropulsionSystemService.selectUavPropulsionSystemByUavId(uavId);
        UavPropulsionSystem uavPropulsionSystemUp = uavEntity.getUavPropulsionSystem();
        uavPropulsionSystemUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavPropulsionSystem)) {
            uavPropulsionSystemService.updateUavPropulsionSystem(uavPropulsionSystemUp);
        }else {
            uavPropulsionSystemService.insertUavPropulsionSystem(uavPropulsionSystemUp);
        }
        /*机身结构系统*/
        UavAirframeConstruction uavAirframeConstruction = uavAirframeConstructionService.selectUavAirframeConstructionByUavId(uavId);
        UavAirframeConstruction uavAirframeConstructionUp = uavEntity.getUavAirframeConstruction();
        uavAirframeConstructionUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavAirframeConstruction)) {
            uavAirframeConstructionService.updateUavAirframeConstruction(uavAirframeConstructionUp);
        }else {
            uavAirframeConstructionService.insertUavAirframeConstruction(uavAirframeConstructionUp);
        }
        /*飞控系统*/
        UavFlightcontrolSystem uavFlightcontrolSystem = uavFlightcontrolSystemService.selectUavFlightcontrolSystemByUavId(uavId);
        UavFlightcontrolSystem uavFlightcontrolSystemUp = uavEntity.getUavFlightcontrolSystem();
        uavFlightcontrolSystemUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavFlightcontrolSystem)) {
            uavFlightcontrolSystemService.updateUavFlightcontrolSystem(uavFlightcontrolSystemUp);
        }else {
            uavFlightcontrolSystemService.insertUavFlightcontrolSystem(uavFlightcontrolSystemUp);
        }

        /*无人机通信与数传*/
        UavCommunicationDatalink uavCommunicationDatalink = uavCommunicationDatalinkService.selectUavCommunicationDatalinkByUavId(uavId);
        UavCommunicationDatalink uavCommunicationDatalinkUp = uavEntity.getUavCommunicationDatalink();
        uavCommunicationDatalinkUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavCommunicationDatalink)) {
            uavCommunicationDatalinkService.updateUavCommunicationDatalink(uavCommunicationDatalinkUp);
        }else {
            uavCommunicationDatalinkService.insertUavCommunicationDatalink(uavCommunicationDatalinkUp);
        }

        /*任务载荷对象*/
        UavPayloadCamera uavPayloadCamera = uavPayloadCameraService.selectUavPayloadCameraByuavId(uavId);
        UavPayloadCamera uavPayloadCameraUp = uavEntity.getUavPayloadCamera();
        uavPayloadCameraUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavPayloadCamera)) {
            uavPayloadCameraService.updateUavPayloadCamera(uavPayloadCameraUp);
        }else {
            uavPayloadCameraService.insertUavPayloadCamera(uavPayloadCameraUp);
        }
        int i = uavTechnicalSpecificationsService.updateUavTechnicalSpecifications(uavTechnicalSpecifications);
        return i;
    }

    @Override
    public int updateUavEntity(UavEntity uavEntity, MultipartFile file) {
        UavTechnicalSpecifications uavTechnicalSpecifications = uavEntity.getUavTechnicalSpecifications();
        String uavId = uavTechnicalSpecifications.getUavId();
        String saveDraft = uavTechnicalSpecifications.getSaveDraft();
        String uavImagePath = uavTechnicalSpecifications.getUavImagePath();

        /*无人机电池*/
        UavBatterySystem uavBatterySystem = uavBatterySystemService.selectUavBatterySystemByUavId(uavId);
        UavBatterySystem uavBatterySystemUp = uavEntity.getUavBatterySystem();
        uavBatterySystemUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavBatterySystem)) {
            uavBatterySystemService.updateUavBatterySystem(uavBatterySystemUp);
        }else {
            uavBatterySystemService.insertUavBatterySystem(uavBatterySystemUp);
        }
        /*无人机动力系统*/
        UavPropulsionSystem uavPropulsionSystem = uavPropulsionSystemService.selectUavPropulsionSystemByUavId(uavId);
        UavPropulsionSystem uavPropulsionSystemUp = uavEntity.getUavPropulsionSystem();
        uavPropulsionSystemUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavPropulsionSystem)) {
            uavPropulsionSystemService.updateUavPropulsionSystem(uavPropulsionSystemUp);
        }else {
            uavPropulsionSystemService.insertUavPropulsionSystem(uavPropulsionSystemUp);
        }
        /*机身结构系统*/
        UavAirframeConstruction uavAirframeConstruction = uavAirframeConstructionService.selectUavAirframeConstructionByUavId(uavId);
        UavAirframeConstruction uavAirframeConstructionUp = uavEntity.getUavAirframeConstruction();
        uavAirframeConstructionUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavAirframeConstruction)) {
            uavAirframeConstructionService.updateUavAirframeConstruction(uavAirframeConstructionUp);
        }else {
            uavAirframeConstructionService.insertUavAirframeConstruction(uavAirframeConstructionUp);
        }
        /*飞控系统*/
        UavFlightcontrolSystem uavFlightcontrolSystem = uavFlightcontrolSystemService.selectUavFlightcontrolSystemByUavId(uavId);
        UavFlightcontrolSystem uavFlightcontrolSystemUp = uavEntity.getUavFlightcontrolSystem();
        uavFlightcontrolSystemUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavFlightcontrolSystem)) {
            uavFlightcontrolSystemService.updateUavFlightcontrolSystem(uavFlightcontrolSystemUp);
        }else {
            uavFlightcontrolSystemService.insertUavFlightcontrolSystem(uavFlightcontrolSystemUp);
        }

        /*无人机通信与数传*/
        UavCommunicationDatalink uavCommunicationDatalink = uavCommunicationDatalinkService.selectUavCommunicationDatalinkByUavId(uavId);
        UavCommunicationDatalink uavCommunicationDatalinkUp = uavEntity.getUavCommunicationDatalink();
        uavCommunicationDatalinkUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavCommunicationDatalink)) {
            uavCommunicationDatalinkService.updateUavCommunicationDatalink(uavCommunicationDatalinkUp);
        }else {
            uavCommunicationDatalinkService.insertUavCommunicationDatalink(uavCommunicationDatalinkUp);
        }

        /*任务载荷对象*/
        UavPayloadCamera uavPayloadCamera = uavPayloadCameraService.selectUavPayloadCameraByuavId(uavId);
        UavPayloadCamera uavPayloadCameraUp = uavEntity.getUavPayloadCamera();
        uavPayloadCameraUp.setSaveDraft(saveDraft);
        if (Objects.nonNull(uavPayloadCamera)) {
            uavPayloadCameraService.updateUavPayloadCamera(uavPayloadCameraUp);
        }else {
            uavPayloadCameraService.insertUavPayloadCamera(uavPayloadCameraUp);
        }
        UavTechnicalSpecifications uavTechnicalSpecificationsOld = iUavTechnicalSpecificationsService.selectUavTechnicalSpecificationsByUavId(uavId);
        String uavImagePathOld = uavTechnicalSpecificationsOld.getUavImagePath();
        /*原数据有图片路径则先删除后更新*/
        if (StringUtils.isNotEmpty(uavImagePathOld)){
            FileUtils.deleteFile(LantianConfig.getProfile() + FileUtils.stripPrefix(uavImagePathOld));
            uavImagePath = updateImage(file);
        }else {
            /*原数据无图片路径则直接更新*/
            uavImagePath = updateImage(file);
        }
        uavTechnicalSpecifications.setUavImagePath(uavImagePath);
        int i = uavTechnicalSpecificationsService.updateUavTechnicalSpecifications(uavTechnicalSpecifications);
        return i;
    }

    private String updateImage(MultipartFile file) {
        String uavImagePath;
        if (!file.isEmpty()){
            try {
                uavImagePath = FileUploadUtils.upload(LantianConfig.getUavTechnicalSpecificationsPath(), file, MimeTypeUtils.IMAGE_EXTENSION, true);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (InvalidExtensionException e) {
                throw new RuntimeException(e);
            }
        }else {
            uavImagePath = null;
        }
        return uavImagePath;
    }

    @Override
    public UavEntity selectUavEntityByUavId(String uavId) {
        UavEntity uavEntity = new UavEntity();
        uavEntity.setUavTechnicalSpecifications(uavTechnicalSpecificationsService.selectUavTechnicalSpecificationsByUavId(uavId));
        uavEntity.setUavBatterySystem(uavBatterySystemService.selectUavBatterySystemByUavId(uavId));
        uavEntity.setUavPropulsionSystem(uavPropulsionSystemService.selectUavPropulsionSystemByUavId(uavId));
        uavEntity.setUavAirframeConstruction(uavAirframeConstructionService.selectUavAirframeConstructionByUavId(uavId));
        uavEntity.setUavFlightcontrolSystem(uavFlightcontrolSystemService.selectUavFlightcontrolSystemByUavId(uavId));
        uavEntity.setUavCommunicationDatalink(uavCommunicationDatalinkService.selectUavCommunicationDatalinkByUavId(uavId));
        uavEntity.setUavPayloadCamera(uavPayloadCameraService.selectUavPayloadCameraByuavId(uavId));
        return uavEntity;
    }
}
